package controller.fileUploadDownload;


import controller.common.BaseController;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import properties.SystemProperty;
import response.CommonResult;
import util.io.FtpUtil;
import util.io.ImgUitl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by Administrator on 2017/10/25.
 */
@Controller
@RequestMapping("imgBase64upLoad")
public class ImgBase64upLoadController extends BaseController {

    @RequiresUser
    @ResponseBody
    @RequestMapping("saveHeadImg")
    public CommonResult saveHeadImg(String sign, String source) {
        CommonResult commonResult = new CommonResult();
        String filePathName = SystemProperty.getLocalFilePath() + File.separator + SystemProperty.getExpertHeadImgFolder() + File.separator + sign + ".jpg";
        System.out.println("filePathName=" + filePathName);
        boolean result = ImgUitl.generateImage(source, filePathName);

        if (result == false) {
            log.error("保存到本地失败  filePathName={}" , filePathName);
            commonResult.setMessage("图片存储失败");
            return commonResult.setError();
        }
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePathName);
        } catch (Exception e) {
            log.error("读取本地图片失败  filePathName={}" , filePathName);
            commonResult.setMessage("图片存储失败");
            return commonResult.setError();
        }
        boolean result1 = FtpUtil.saveFileByRelativePath(fileInputStream, SystemProperty.getExpertHeadImgFolder(), sign + ".jpg");
        if (result1 == false) {
            log.error("保存到Ftp失败  filePathName={}" , filePathName);
            commonResult.setMessage("图片存储失败");
            return commonResult.setError();
        }


        commonResult.setData("downloadFileByPath?pathName=" + SystemProperty.getExpertHeadImgFolder() + "/" + sign + ".jpg");

        return commonResult.setSuccess();

    }


    public static void main(String[] args) throws Exception {
//        System.out.println("dd");
        File file = new File("c:\\asfds1a\\fdsfsd1f\\j1dbc.properties");
        file.mkdirs();
        File f1 = new File("C:\\jdbc.properties");
        byte[] g = new byte[10000];
        new FileInputStream(f1).read(g);
        new FileOutputStream(file).write(g);
        System.out.println(file.mkdirs());
        System.out.println(file.isDirectory());


        System.out.println(file.exists());

    }

}
